<template>
  <div class="app-container">
    <el-form ref="form" :model="form" label-width="100px" class="demo-ruleForm">
      <el-row>
        <el-col :span="16">
          <el-form-item label="模板名" prop="name">
            <el-input v-model="form.name" placeholder="请输入内容" maxlength="20" show-word-limit />
          </el-form-item>
          <el-form-item label="YAML模板" prop="yaml">
            <el-input v-model="form.yaml" type="textarea" rows="30" />
          </el-form-item>
          <el-form-item label="备注" prop="comment">
            <el-input v-model="form.comment" placeholder="请输入内容" type="textarea" maxlength="120" rows="5" show-word-limit />
          </el-form-item>

        </el-col>

      </el-row>
      <el-form-item>
        <el-button type="primary" @click="submitForm('form')">提交</el-button>
        <el-button @click="resetForm('form')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getRow, addRow, editRow } from '@/api/setting/yamltemplate'

export default {
  data() {
    return {
      form: {}
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      const id = this.$route.query.id
      if (id !== undefined) {
        getRow({ id: id }).then(response => {
          this.form = response
        })
      }
    },
    submitForm() {
      if (this.form.id !== undefined) {
        editRow(this.form.id, this.form).then(response => {
          this.$message({
            type: 'success',
            message: '编辑成功'
          })
          this.$router.go(-1)
        })
      } else {
        addRow(this.form).then(response => {
          this.$message({
            type: 'success',
            message: '添加成功'
          })
          this.$router.go(-1)
        })
      }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>
